<?php

namespace addons\shopro\controller;

use app\admin\model\car\Brand;
use app\admin\model\car\Carlist;
use app\admin\model\car\Chelistsku;
use app\common\model\shopro\CarDetailsModel;
use app\common\model\shopro\CarVehicleModel;
use think\Db;
use app\common\library\Ems;
class Car extends Common
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * @menu 获取品牌
     * ''
     * @Date:2024-05-21 21:32
     */

    public function index()
    {
        $model = new Brand();

        $order = input('order');
        if($order){
            $order = 'initial asc';
        }else{
            $order = 'weigh desc';
        }
        $data  = $model->where('status','=','normal')->order($order)->paginate(request()->param('list_rows', 10));
        $this->success('获取成功', $data);
    }

    /**
     * @menu 根据品牌获取车型
     * ''
     * @Date:2024-05-21 21:51
     */
    public function car_data(){
        $car_brand_id = input('car_brand_id');
        if(!$car_brand_id){
            $this->error('缺少参数');
        }
        $model = new CarVehicleModel();
        $data = $model->where(['car_brand_id'=>$car_brand_id])->paginate();
        $data = $data->toArray();
        if($data['data']){
            foreach ($data['data'] as $k=>&$v){
                $parentid = Carlist::where(['car_ids'=>$v['one_id']])->value('id');
                if($parentid){
                    $skuInfo = Chelistsku::where(['car_list_id'=>$parentid])->order('id asc')->find();
                    $v['price'] = $skuInfo['price'];
                    $v['sizetype'] = $skuInfo['sizetype'];
                }
            }
        }
        $this->success('ok',$data);
    }

    /**
     * 汽车列表
     */

    public function car_list(){
        $data = Carlist::where(['status'=>'normal'])->paginate();
        $data = $data->toArray();
        if($data['data']){
            foreach ($data['data'] as $k=>&$v){
                $skuInfo = Chelistsku::where(['car_list_id'=>$v['id']])->order('id asc')->find();
                $v['price'] = $skuInfo['price'];
                $v['sizetype'] = $skuInfo['sizetype'];
            }
        }
        $this->success('ok',$data);
    }

    /**
     * 根据车型获取汽车sku
     */

    public function car_sku(){
        $ids  = input('ids',0);
        $type  = input('type',0);
        $sizetype = input('sizetype');
        if($type){
            $ids = Carlist::whereIn('car_ids',$ids)->column('id');
        }
//        $where['car_list_id'] = ['in',$ids];
        if($sizetype){
            $where['sizetype'] = $sizetype;
        }
        $image = [];
        $data = db('shopro_car_list_sku')->whereIn('car_list_id', $ids)->order('price asc')->select();
        $cat_type = [];
        $price    = '';
        $canpei   = '';
        if($data){
            foreach ($data as $k=>$v){
                if(!isset($cat_type[$v['sizetype']])){
                    $cat_type[$v['sizetype']] = $v['sizetype'];
                }
                $image[$k] = $v['logo'];
            }
            $num = count($data);
            $price = $data[0]['price'] .'~'.$data[$num-1]['price'];
            $canpei = CarDetailsModel::where(['car_id'=>$data[0]['car_id']])->find();
        }
        $this->success('ok',['type'=>$cat_type,'price'=>$price,'image'=>$image,'data'=>$data,'canpei'=>$canpei,]);
    }


    public function car_details(){
        $ids  = input('ids');
        $data = CarDetailsModel::whereIn('car_id',$ids)->group('car_id')->select();
        $this->success('ok',$data);
    }

}
